################################
# CREATE JPOP SIMULATIONS FILE #
################################

# Author: Kasia Nalewajko
# First version: 29 November 2023
# Replicated: 13 June 2024

rm(list = ls())

# LOAD DATA ---------------------------------------------------------------

load("./00 SUBMITTED/00 APSR final/04 replication_files/01 data/main.Rda")

# Assign Jewish population to counties randomly ----

num_repetitions <- 1500
seed_sequence <- 1:1500
simulation_dfs_list <- list()

for (i in 1:num_repetitions) {
  set.seed(seed_sequence[i])
  var_name <- paste0("jpop_random_distribution_", i)
  simulation_dfs_list[[i]] <- main %>% 
    dplyr::select(deppct, dept_code, number_jews_above_15_dep, number_counties_in_dept) %>% 
    group_by(dept_code) %>% 
    mutate(random_proportions = runif(number_counties_in_dept, min = 0, max = 1),
           normalised_random_proportions = random_proportions/sum(random_proportions),
           !!var_name := number_jews_above_15_dep * normalised_random_proportions) %>% 
    dplyr::select(-c(number_jews_above_15_dep, number_counties_in_dept, random_proportions, normalised_random_proportions))
  
  print(i)
}

for_simulations <- data.frame(simulation_dfs_list) %>% 
  dplyr::select(deppct, starts_with(match = "jpop_random_distribution"))

# save(for_simulations, file = "./00 SUBMITTED/00 APSR final/04 replication_files/01 data/jpop_simulations.Rda")
